
//
// 110615 - AH - (Bien *) Created.
// 110617 - AH - Finished implementation.
//

package org.soekrat.gui.swing.resource;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JPanel;

import org.soekrat.engine.Soekrat2Engine;
import org.soekrat.engine.SoekratName;
import org.soekrat.engine.mine.Resource;

/**
 * <p>
 *   Panel with tree view of 
 *   {@link Resource} and tags and with buttons to create and remove.
 * </p>
 * @author Arne Halvorsen (AH) 
 */
@SuppressWarnings("serial")
public final class MineFanPanel extends JPanel
{
  private final class CreateResourceAction extends AbstractAction
  {
    public CreateResourceAction()
    { 
      super("Create Resource");
    }
    
    public void actionPerformed(ActionEvent e){ m_rtp.createResource(); }
    
  }
  
  private final class CreateTagAction extends AbstractAction
  {
    public CreateTagAction()
    { 
      super("Create tag");
    }
    
    public void actionPerformed(ActionEvent e) 
    {
      Resource rsel=m_rtp.getSelectedResource();
      if (rsel!=null) m_rtp.createTag((Resource)rsel);
    }
    
  }
  
  private final class RemoveResourceAction extends AbstractAction
  {
    public RemoveResourceAction()
    { 
      super("Remove Resource");
    }
    
    public void actionPerformed(ActionEvent e) 
    {
      Resource rsel=m_rtp.getSelectedResource();
      if (rsel!=null) m_rtp.removeResource(rsel);
    }
    
  }
  
  private final class RemoveTagAction extends AbstractAction
  {
    public RemoveTagAction()
    { 
      super("Remove Tag");
    }
    
    public void actionPerformed(ActionEvent e) 
    {
      Resource rsel=m_rtp.getSelectedResource();
      if (rsel!=null)
      { 
        SoekratName rtag=m_rtp.getSelectedTag();
        if (rtag!=null) m_rtp.removeTag(rsel, rtag);
      }
    }
    
  }
  
  private final ResourceTreePanel m_rtp;
  
  /**
   * <p>
   *   Constructor.
   * </p>
   * @param engine {@link Soekrat2Engine} interfaced.
   */
  public MineFanPanel(Soekrat2Engine engine)
  {
    if (engine==null)
    {
      throw new NullPointerException("engine");
    }
    
    setLayout(new BorderLayout());
 
    m_rtp=new ResourceTreePanel(engine);
    add(m_rtp, BorderLayout.CENTER);
    
    JPanel buttons=new JPanel(new FlowLayout(FlowLayout.CENTER));
    buttons.add(new JButton(new CreateResourceAction()));
    buttons.add(new JButton(new CreateTagAction()));
    buttons.add(new JButton(new RemoveResourceAction()));
    buttons.add(new JButton(new RemoveTagAction()));
    add(buttons, BorderLayout.SOUTH);
  }
  
}
